/*
exampleBBSelector: example of use for Backbones Selector project.
    Copyright (C) 2010  Jorge E. A. Atala
                        Adrian Remonda
                        FuDePAN
    This file is part of the Backbones-Selector Proyect.

    Backbones-Selector is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Backbones-Selector is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Backbones-Selector.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <iostream>
#include <sys/time.h>
#define MILI_NAMESPACE
#include <mili/mili.h>
using namespace mili;
#include <feca/feca.h>
#include <prot-filer/xtc_filer.h>
#include <prot-filer/cached_reader.h>
#include "backbones-selector/ranker/backbones-selector.h"

//Compile with -lprot-filer option!!!!!

using namespace  backbonesSelector;
using namespace prot_filer;


double wtime(void)
{
    struct timeval tv;
    gettimeofday(&tv, 0);

    return static_cast<double>(tv.tv_sec + 1e-6 * tv.tv_usec);
}

int main()
{
    double start_t = 0.0;
    double time_acum = 0.0;
    //Create an energy Table from data file
    const EnergyTable<float, float> energyTable("tests/ref/matrixData.txt");

    //create an example aminoacid Sequence
    const AminoacidSequence aminoacidsSt = "AWYCDMQNKHVVRGPTDQEMTAHQWKRNWWHGNKRYCCPDQEEVC";

    //Create ranker
    Ranking proteinRanker(10);

    //Create selector
    backbonesSelector::Selector<float, float> machine(energyTable, proteinRanker, aminoacidsSt);

    //read structures from xtc file and add structures to the selector,
    //when an Structure is added, the selector work with the ranker
    //and make a ranking with the best Structures
    CachedReader<FullCache, Coord3DReader,  Structure< > > r(Coord3DReaderFactory::get_instance()->create("xtc"));
    r.open("tests/ref/test.xtc");
    unsigned int i = 0;
    Structure< >* protein = NULL;

    while ((protein = r.read(i)) != NULL)
    {
        start_t = wtime();
        machine.addStructure(*protein);
        ++i;
        time_acum += (wtime() - start_t);
    }
    std::cout << time_acum << std::endl;
    r.close();

    //Do something with the result (i.e. show the energy for the top ten proteins)
    mili::CAutonomousIterator<Ranking> rankingIterator(proteinRanker);
    while (!rankingIterator.end())
    {
        std::cout << (*rankingIterator)->getEnergy() << "\n";
        ++rankingIterator;
    }
    return EXIT_SUCCESS;
}
